<?php if (!defined('BASEPATH'))

    exit('No direct script access allowed');

require_once APPPATH . "controllers/super" . EXT;





class User extends super{

    

    function __construct(){

        parent::__construct();

        $this->load->model('user_model','',TRUE);

    }

    

    public function template(){

        $data = array();

        $this->load->helper(array('form'));

        $data['title_page'] = 'User';

        $data['header']= $this->header('User');

        $data['footer']= $this->footer();

        $data['navigate']= $this->navigate();

        $data['js'] = $this->load->view('user/js',null,true);

        $data['sidebarleft'] = $this->load->view('user/sidebarleft',$data,true);

        return $data;

    }

    

    //Show message success or error

    public function setFlash($message, $type){

        $this->session->set_userdata('typeMess', $type);

        $this->session->set_userdata('message',$message); 

    }

    

    //validate is unique Name and check username valid (not allow space)
    public function checkUsernameExist($username){
        $result = $this->user_model->getSingleUser(array('username' => $username));
        if(!preg_match("/^\S+$/", $username)){
            $this->form_validation->set_message('checkUsernameExist', 'Username not allow space between characters');
            return false;   
        }if(!empty($result)){
            $this->form_validation->set_message('checkUsernameExist', 'This username has already used');
            return false;
        }else{
            return true;    
        }
        
    }
    //validate confirm password
    public function checkMatchPassword($confirmpass){
        $password = $this->input->post('password');
        if($password == $confirmpass){
            return true;
        }else{
            $this->form_validation->set_message('checkMatchPassword', 'Confirm password not match');
            return false;
        }
    }
    
    //check old password  = new password
    public function checkOldPassword($password){
        $session_data = $this->session->userdata('logged_in');
        $id = $session_data['id'];
        $currentuser = $this->user_model->getSingleUser(array('iduser'=>$id));
        if(MD5($password) != $currentuser['password']){
            $this->form_validation->set_message('checkOldPassword', 'Password wrong, please try again');
            return false;
        }else{
            return true;
        }
    }
    
    public function listUser(){

        $data = $this->template();  
        
        $data['list'] = $this->user_model->getListUser();

        $this->load->view('user/listUser_view', $data);

    }

    

    public function addUser(){

        $data = array();

        $data = $this->template();

        $this->form_validation->set_rules('username', 'User Name', 'trim|required|min_length[1]|xss_clean|callback_checkUsernameExist');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[1]|xss_clean');
        $this->form_validation->set_rules('confirmpassword', 'Confirm Password', 'trim|required|min_length[1]|xss_clean|callback_checkMatchPassword');
        
        if($this->input->post()){

            $status = $this->input->post('status');

            if(empty($status)){

                $status = '0';

            }

            $param['username'] = $this->input->post('username');
            
            $param['password'] =  MD5($this->input->post('password'));
            
            $param['active'] = $status;

            $param["dateupdated"]   = time();

            $param["datecreated"]   = time();

            //nê´u validate co´ lô~i

            if($this->form_validation->run() == FALSE){

                $this->load->view('user/addUser_view', $data);

            }else{

                // nê´u thêm department tha`nh công

                if($this->user_model->addUser($param)){

                    $this->setFlash('Success register new user', 'success');

                    redirect('user/listUser','refesh');

                //nguoc lai       

                }else{

                    $this->setFlash('Error for register new user, please try again', 'error');

                    redirect('user/addUser', 'refesh');

                }

            }

        }else{

            $this->load->view('user/addUser_view', $data);

        } 

    }
    
    public function changePassword($id){
        $data = $this->template();
        if($this->input->post()){
            $this->form_validation->set_rules('oldpassword', 'Old Password', 'trim|required|min_length[1]|xss_clean|callback_checkOldPassword');
            $this->form_validation->set_rules('password', 'New Password', 'trim|required|min_length[1]|xss_clean');
            $this->form_validation->set_rules('confirmpassword', 'Confirm Password', 'trim|required|xss_clean|min_length[1]|callback_checkMatchPassword');
            
            if($this->form_validation->run() == FALSE){
                $this->load->view('user/changePassword_view', $data);
            }else{
                $param['password'] = MD5($this->input->post('password'));
                
                if($this->user_model->changePass($param, array('iduser'=>$id))){
                    $this->setFlash('Change Password Successfully, please login again!', 'success');
                    redirect('main/logout','refesh');
                }else{
                    $this->setFlash('Change Password failed!', 'error');
                    redirect('user/listUser','refesh');
                } 
            }
        }else{
            $this->load->view('user/changePassword_view', $data);
        }
        
        
    }
    
    public function changeStatus($id){
        $data = array();
        $data['user'] = $this->user_model->getSingleUser(array('iduser'=>$id));

        if(!empty($data['user'])){
            $condition = array('iduser'=>$id);
            if($data['user']['active'] == 1){
                $param['active'] = 0;
                $message = 'Disable User successfully';
                   
            }else{
                $param['active'] = 1;
                $message = 'Active User successfully';
            }
            if($this->user_model->changePass($param,$condition)){
                $this->setFlash($message, 'success');
            }else{
                $this->setFlash('Error change Status User', 'error');
            }
        }else{
            $this->setFlash('Not found this User', 'error');
        }
        redirect('user/listUser', 'refesh');
    }
    
    public function deleteUser($id){

        if($this->user_model->deleteUser(array('iduser'=>$id))){

            $this->setFlash('Success delete user', 'success');

        }else{

            $this->setFlash('Can not delete this user', 'error');

        }

        redirect("user/listUser", 'refresh');

    }


}